//
//  THHomeSegeViewController.m
//  TreeHole
//
//  Created by apple on 2020/7/27.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THOtherUserInfoViewController.h"
#import "THOtherUserInfoChildTieziController.h"
#import "THOtherUserInfoChildVideoController.h"

#import "THOtherUserInfoChildAttentionController.h"
#import "THOtherUserInfoChildFensController.h"

@interface THOtherUserInfoViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) UIView                     *headerView;/**<  */
@property(nonatomic, strong) STTools_SegementAutoView                     *segView;/**<  */
@property(nonatomic, strong) STButton                     *iconButton;/**<  */
@property(nonatomic, strong) THUserModel                     *model;/**<  */
@end

@implementation THOtherUserInfoViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    self.navigationController.navigationBar.hidden = YES;
    self.cus_navgationBar.backgroundColor = UIColor.clearColor;
    [self.cus_navgationBar.leftButton setImage:ST_IMAGE_NAMED(@"video_返回") forState:UIControlStateNormal];
    [THUserDataController stoneobs_fun_send_get_user_ifno_request_id:self.user_id handle:^(THUserModel * _Nonnull user) {
        self.model = user;
        [self config_base_view];
    }];

    // Do any additional setup after loading the view.
}
- (void)config_base_view{
    UIImageView * topView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, 230)];
    topView.backgroundColor = WH_UICOLOR_FROM_RGBA(0x33373B);
    topView.userInteractionEnabled = YES;
    topView.contentMode = UIViewContentModeScaleAspectFill;
    topView.clipsToBounds = YES;
    [topView sd_setImageWithURL:_model.bg.mj_url];
    [self.view addSubview:topView];
    self.headerView = topView;
    

    CGFloat top = 0;
    __weak typeof(self) weakSelf =  self;

    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(10, 8 + TMUtils.stoneobs_fun_get_navgationBarBootom, 70, 70)
                                                                  title:nil
                                                             titleColor:nil
                                                              titleFont:0
                                                           cornerRadius:35
                                                        backgroundColor:nil
                                                        backgroundImage:STFDRandomImageProvider.random_livegirl_image
                                                                  image:nil];
        
        [stoneobs_ui_button sd_setImageWithURL:self.model.header_img.mj_url forState:UIControlStateNormal];
        [topView addSubview:stoneobs_ui_button];
        self.iconButton = stoneobs_ui_button;
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {

        }];
        
        
    });

    ({
        UIImageView * codeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
        codeImageView.contentMode = UIViewContentModeScaleAspectFill;
        codeImageView.clipsToBounds = YES;
//        [self.view addSubview:codeImageView];
        codeImageView.userInteractionEnabled = YES;
        UITapGestureRecognizer * codeTap = [[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            STTools_WebViewController * webView =  [STTools_WebViewController.alloc initWithUrl:weakSelf.model.QrCode];
            webView.forceTitle = @"二维码";
            [weakSelf pushViewController:webView animated:1];
        }];
        [codeImageView addGestureRecognizer:codeTap];
        codeImageView.right = UISCREEN_WIDTH - 15;
        codeImageView.centerY = self.cus_navgationBar.leftButton.centerY;
        self.cus_navgationBar.rightButton.hidden = YES;
//        [self.cus_navgationBar addSubview:codeImageView];
        
        [codeImageView sd_setImageWithURL:self.model.QrCode.mj_url];
        
    });
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(self.iconButton.right + 10, self.iconButton.top+10, 200, 20)
                                                                text:self.model.name
                                                           textColor:UIColor.whiteColor
                                                                font:15
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [topView addSubview:stoneobs_ui_label];
        stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:15];
    });

    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(self.iconButton.right + 10, 0, 41, 20)
                                                                  title:self.model.cus_sex_string
                                                             titleColor:STONEOBS_UI_SECEND_TEXT_COLOR
                                                              titleFont:12
                                                           cornerRadius:10
                                                        backgroundColor:UIColor.whiteColor
                                                        backgroundImage:nil
                                                                  image:ST_IMAGE_NAMED(@"女")];
        stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        stoneobs_ui_button.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
        [stoneobs_ui_button setImage:ST_IMAGE_NAMED(@"男") forState:UIControlStateSelected];
        [topView addSubview:stoneobs_ui_button];
        stoneobs_ui_button.bottom = self.iconButton.bottom;
        if (self.model.sex.intValue == 1) {
            stoneobs_ui_button.selected = YES;
        }
        if (!self.model.is_sex_show) {
            stoneobs_ui_button.hidden = YES;
        }
        NSString * birthday = self.model.birthday;
        if (!self.model.is_birthday_show) {
            birthday = @"该用户设置无法查看生日";
        }
        
        NSString * school = self.model.school;
        if (!self.model.is_school_show) {
            school = @"该用户设置无法查看学校";
        }
        NSString * title = [NSString stringWithFormat:@"生日:%@\n学校:%@\n简介:%@ \n身份:%@",birthday,school,self.model.brief,self.model.level_text];
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(self.iconButton.right + 10, stoneobs_ui_button.bottom + 10, 200, 20)
                                                                text:title
                                                           textColor:UIColor.whiteColor
                                                                font:13
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stoneobs_ui_label];
        [self.headerView addSubview:stoneobs_ui_label];
        
        top = stoneobs_ui_label.bottom + 30;
    });
    

    STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 80, 41)
                                                              title:@"+关注"
                                                         titleColor:UIColor.whiteColor
                                                          titleFont:15
                                                       cornerRadius:4
                                                    backgroundColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR
                                                    backgroundImage:nil
                                                              image:nil];
    stoneobs_ui_button.centerY = self.iconButton.centerY;
    stoneobs_ui_button.right = UISCREEN_WIDTH - 15;
    [stoneobs_ui_button setClicAction:^(UIButton *sender) {
        
        [THTreeHoleDataController addAttentionOrCancleFormUser_id:weakSelf.model.uid handle:^(NSError *error, BOOL success, NSDictionary *resp) {
           
            sender.selected = !sender.selected;
            weakSelf.model.is_follow =  !weakSelf.model.is_follow;
            if (weakSelf.model.is_follow) {
                sender.backgroundColor = WH_UICOLOR_FROM_RGBA(0xEEEEEE);
            }else{
                sender.backgroundColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
            }
        }];
    }];
    [stoneobs_ui_button setTitleColor:WH_UICOLOR_FROM_RGBA(0x9A9A9A) forState:UIControlStateSelected];
    [stoneobs_ui_button setTitle:@"已关注" forState:UIControlStateSelected];
    stoneobs_ui_button.selected = self.model.is_follow;
    if (stoneobs_ui_button.selected) {
        stoneobs_ui_button.backgroundColor = WH_UICOLOR_FROM_RGBA(0xEEEEEE);
    }
    [topView addSubview:stoneobs_ui_button];
    stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;


    
    self.segView = [[STTools_SegementAutoView alloc] initWithFrame:CGRectMake(0, top, UISCREEN_WIDTH , 44) andTitle:@[@"图文",@"视频",@"ta的关注",@"ta的粉丝"] handle:^(STTools_SegementAutoView *sender, UIButton *currentSelctedButton) {
        
        [weakSelf.scrollView setContentOffset:CGPointMake(sender.cureentIndex*UISCREEN_WIDTH, 0) animated:YES];

    }];

    [topView addSubview:self.segView];

    self.segView.lineSelectedColor = STONEOBS_UI_THEME_BACKGROUNDCOLOR;
    self.segView.lineColor = UIColor.clearColor;
    self.segView.autoMoveWithClic = YES;
    self.segView.backgroundColor = topView.backgroundColor;
    self.segView.butTitleSelectedColor = UIColor.whiteColor;
    self.segView.butTitleColor = STONEOBS_UI_THIRD_TEXT_COLOR;
    self.segView.butTitleFont = [UIFont systemFontOfSize:12];
    self.segView.butTitleSelectedFont = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:14];
    self.segView.buttonBackGroundColor = UIColor.clearColor;
    self.segView.buttonSelctedBackGroundColor = UIColor.clearColor;
    
    self.headerView.height = self.segView.bottom;
    [self stoneobs_ui_configSubView];
}
#pragma mark --configSubView
- (void)stoneobs_ui_configSubView{
    
    self.scrollView = [UIScrollView.alloc initWithFrame:CGRectMake(0, self.headerView.bottom, UISCREEN_WIDTH, TMUtils.stoneobs_fun_get_tabBarTop+49 - self.headerView.bottom)];
    if (@available(iOS 11.0, *)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.view addSubview:self.scrollView];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    NSInteger i = 0;
    ({
        THOtherUserInfoChildTieziController * vc = THOtherUserInfoChildTieziController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    ({
        THOtherUserInfoChildVideoController * vc = THOtherUserInfoChildVideoController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
   
    ({
        THOtherUserInfoChildAttentionController * vc = THOtherUserInfoChildAttentionController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    
    ({
        THOtherUserInfoChildFensController * vc = THOtherUserInfoChildFensController.new;
        [self addChildViewController:vc];
        [self.scrollView addSubview:vc.view];
        vc.view.left = UISCREEN_WIDTH * i;

        i++;
    });
    self.scrollView.contentSize = CGSizeMake(UISCREEN_WIDTH * i, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.alwaysBounceVertical = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == self.scrollView) {
        NSInteger index = scrollView.contentOffset.x / UISCREEN_WIDTH;
        self.segView.cureentIndex = index;
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if (scrollView == self.scrollView) {
        NSInteger index = scrollView.contentOffset.x / UISCREEN_WIDTH;
        self.segView.cureentIndex = index;
    }
}
@end
